import request from '../../../utils/request'
Page({
  data: {
    weight: '50.0',
    weightStatus: '正常',
    weightTime: '2020/09/09 10:20',

    bloodSugar: '9.6',
    bloodSugarStatus: '偏低',
    bloodSugarTime: '2020/09/09 10:20',

    bloodPressure: '120/80',
    bloodPressureStatus: '正常',
    bloodPressureTime: '2020/09/09 10:20',

    heartRate: '90',
    heartRateStatus: '正常',
    heartRateTime: '2020/09/09 10:20'
  },

  onLoad: function() {
    // 页面加载时获取最新数据
    this.fetchLatestData();
    //血压
    this.getBloodPressure()
    .then(bloodPressure=>{
        this.setData({
            bloodPressure:bloodPressure
        })
    })
    //体重
    this.getUserWeight()
    .then(userweight =>{
        this.setData({
            userweight:userweight
        })
    })

    //血糖
    this.getBloodSuger()
    .then(bloodsuger=>{
        this.setData({
            bloodsuger:bloodsuger
        })
    })
    //心率
    this.getHeartRate()
    .then(heartrate=>{
        this.setData({
            heartrate:heartrate
        })
    })
  },
//获取用户血压
getBloodPressure(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let bloodPressureid = 3  //数据库中血压id=3
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${bloodPressureid}`
        request({
            url:url,
            method:'GET',
            success(res){
                let bloodPressure = res.data
                console.log("血压列表：",bloodPressure);
                resolve(bloodPressure)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  //获取用户血糖
  getBloodSuger(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let bloodsugerid = 2  //数据库中血糖id=2
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${bloodsugerid}`
        request({
            url:url,
            method:'GET',
            success(res){
                // console.log("血糖列表：",res.data);
                let bloodsuger = res.data
                resolve(bloodsuger)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  //获取用户体重
  getUserWeight(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let weightid = 1  //数据库中体重id=1 
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${weightid}`
        request({
            url:url,
            method:'GET',
            success(res){
                // console.log("体重列表：",res.data);
                let userweight = res.data
                resolve(userweight)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  //获取用户心率
  getHeartRate(){
    return new Promise((resolve,reject)=>{
        let userId = wx.getStorageSync('userid')
        let heartrateid = 4  //数据库中心率id=4
        let url = `healthlog/healthlog/selectByUseridAndTypeid?userid=${userId}&typeid=${heartrateid}`
        request({
            url:url,
            method:'GET',
            success(res){
                // console.log("体重列表：",res.data);
                let heartrate = res.data
                resolve(heartrate)
            },fail(error){
                reject(error)
            }
        })
    })
  },
  onShow: function() {
    // 页面显示时刷新数据
    this.fetchLatestData();
  },

  fetchLatestData: function() {
    // 这里调用API获取最新的健康监测数据
    // wx.request({
    //   url: 'your-api-url/health-monitoring/latest',
    //   success: (res) => {
    //     this.setData({
    //       weight: res.data.weight.value,
    //       weightStatus: res.data.weight.status,
    //       weightTime: res.data.weight.time,
    //       // ... 其他数据
    //     });
    //   }
    // });
  },

  // 导航到体重详情页
  navigateToWeight: function() {
    wx.navigateTo({
      url: '/pages/index/healthmonitoring/weight/weight'
    });
  },

  // 导航到血糖详情页
  navigateToBloodSugar: function() {
    wx.navigateTo({
      url: '/pages/index/healthmonitoring/bloodsuger/bloodsuger'
    });
  },

  // 导航到血压详情页
  navigateToBloodPressure: function() {
    wx.navigateTo({
      url: '/pages/index/healthmonitoring/bloodpressure/bloodpressure'
    });
  },

  // 导航到心率详情页
  navigateToHeartRate: function() {
    wx.navigateTo({
      url: '/pages/index/healthmonitoring/heartrate/heartrate'
    });
  }
}); 